﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Edit_AssemblyCheckFunctions : Form
    {
        public string func_id;
        public string checktype_id;

        public frm_Edit_AssemblyCheckFunctions()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.func_id = null;
            this.checktype_id = null;

            LoadCheckType();
        }

        //////////////////////////////////////////
        /// Load Data To Edit
        //////////////////////////////////////////
        public bool LoadData()
        {
            bool ret = true;
            if (string.IsNullOrEmpty(this.checktype_id) || string.IsNullOrEmpty(func_id))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "SELECT * FROM [231_CheckFuntions]"
                        + " WHERE FunctionID = " + this.func_id + " AND CheckTypeID = " + this.checktype_id;
                    SqlDataReader dataReader = sql_cmd.ExecuteReader();
                    if (dataReader.Read())
                    {
                        for (int i = 0; i < this.checkTypeID.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.checkTypeID.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["CheckTypeID"].ToString())
                            {
                                this.checkTypeID.SelectedIndex = i;
                            }
                        }
                        this.functionID.Text = dataReader["FunctionID"].ToString();
                        this.functionName.Text = dataReader["FunctionName"].ToString();

                        int func_type = 0;
                        Int32.TryParse(dataReader["FunctionType"].ToString(), out func_type);
                        if (func_type != 2)
                        {
                            this.functionType.Properties.Items[1].Enabled = false;
                        }
                        if (func_type <= 4)
                        {
                            this.functionType.SelectedIndex = func_type - 1;                            
                        }
                        else
                        {
                            this.functionType.SelectedIndex = 3;                            
                        }
                        if ((bool)dataReader["ShowForm"])
                        {
                            this.showOnForm.Checked = true;
                        }
                        else
                        {
                            this.showOnForm.Checked = false;
                        }

                        this.defaultValue.Text = dataReader["DefaultValue"].ToString().Trim();
                    }
                    dataReader.Close();
                    sql_cmd.Dispose();
                }
                catch (Exception ex)
                {
                    ret = false;
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    myConnection.Close();
                }
            }
            return ret;
        }

        //////////////////////////////////////
        /// Load Check Type to List
        //////////////////////////////////////
        private void LoadCheckType()
        {
            try
            {
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT * FROM [231_CheckType] ORDER BY CheckTypeName";
                SqlDataReader dataReader = sql_cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["CheckTypeID"].ToString().Trim();
                    item.Text = dataReader["CheckTypeName"].ToString().Trim();
                    this.checkTypeID.Properties.Items.Add(item);
                }
                dataReader.Close();
                sql_cmd.Dispose();
            }
            catch
            {
                MessageBox.Show("Cannot load Check Type");
            }
            finally
            {
                myConnection.Close();
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }        
        
        ///////////////////////////
        /// When Click Add Button
        ///////////////////////////        
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string err = null;            
            int funciontype = 0;
            int showform = 0;
            string functionname = null;
            string defaultvalue = null;

            funciontype = this.functionType.SelectedIndex + 1;

            if (string.IsNullOrEmpty(err))
            {
                if (string.IsNullOrEmpty(this.functionName.Text.Trim()))
                {
                    err = "Please enter Function Name";
                }
                else
                {
                    functionname = this.functionName.Text.Trim();
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                if (this.showOnForm.Checked)
                {
                    showform = 1;
                }
                else
                {
                    showform = 0;
                }
                defaultvalue = this.defaultValue.Text.Trim();
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "UPDATE [231_CheckFuntions]"
                        + " SET FunctionName = N'" + functionname + "', FunctionType = " + funciontype + ","
                        + " ShowForm = " + showform + ", DefaultValue = N'" + defaultvalue + "'"
                        + " WHERE FunctionID = " + this.func_id + " AND CheckTypeID = " + this.checktype_id;
                    sql_cmd.ExecuteNonQuery();
                    sql_cmd.Dispose();
                    MessageBox.Show("Update Function successfully.");
                    this.Dispose();
                }
                catch
                {
                    MessageBox.Show("Cannot update Function.");
                }
                finally
                {
                    myConnection.Close();
                }
            }
            else
            {
                MessageBox.Show(err);
            }
        }
    }
}
